/*
 * $Header: /data/cvs/alin/nanhill-platform/src/test/java/org/nanhill/commons/dbutil/MockResultSetMetaData.java,v 1.1 2006/09/20 06:56:23 alin Exp $
 * $Revision: 1.1 $
 * $Date: 2006/09/20 06:56:23 $
 * 
 */

package org.nanhill.commons.dbutil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.ResultSetMetaData;

import org.nanhill.commons.dbutil.ProxyFactory;

/**
 * MockResultSetMetaData dynamically implements the ResultSetMetaData interface.
 * 
 * @author alin
 * 
 */
public class MockResultSetMetaData implements InvocationHandler {

	private String[] columnNames = null;

	/**
	 * Create a <code>MockResultSetMetaData</code> proxy object. This is
	 * equivalent to:
	 * 
	 * <pre>
	 * ProxyFactory.instance().createResultSetMetaData(
	 * 		new MockResultSetMetaData(columnNames));
	 * </pre>
	 * 
	 * @param columnNames
	 * @return
	 */
	public static ResultSetMetaData create(String[] columnNames) {
		return ProxyFactory.instance().createResultSetMetaData(
				new MockResultSetMetaData(columnNames));
	}

	public MockResultSetMetaData(String[] columnNames) {
		super();
		this.columnNames = columnNames;

	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {

		String methodName = method.getName();

		if (methodName.equals("getColumnCount")) {
			return new Integer(this.columnNames.length);

		} else if (methodName.equals("getColumnName")
				|| methodName.equals("getColumnLabel")) {

			int col = ((Integer) args[0]).intValue() - 1;
			return this.columnNames[col];

			// stub out other methods for now
		} else {
			Class returnType = method.getReturnType();

			if (returnType.equals(String.class)) {
				return "";

			} else if (returnType.equals(Integer.TYPE)) {
				return new Integer(0);

			} else {
				return Boolean.FALSE;
			}
		}
	}
}